package com.djinfo.work.service.impl;

import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.dao.impl.NoticeDaoImpl;
import com.djinfo.work.entity.SysFileEntity;
import com.djinfo.work.entity.SysMessageEntity;
import com.djinfo.work.entity.SysNoticeEntity;
import com.djinfo.work.service.AttachmentService;
import org.springframework.stereotype.Service;


/**
 *
 */
@Service("AttachmentService")
public class AttachmentServiceImpl extends BaseServiceImpl<SysFileEntity,NoticeDaoImpl> implements AttachmentService {

	/**
	 * 批量或者单个删除附件
	 * @param id
     */
	@Override
	public void delSomeData(String id) {
		try {
			String hql = "delete from SysFileEntity where tableid='"+id+"'";
			this.delSome(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改公告里的附件
	 * @param sysNoticeEntity
	 * @param lengths
	 * @param nowTime
     * @param id
     */
	@Override
	public void updateAttach(SysNoticeEntity sysNoticeEntity, int lengths, String nowTime, String id) {
		try {
			if(lengths>0){
                String file = sysNoticeEntity.getAttachMent().substring(0,lengths-1);
                String[] filess=file.split(",");
                for(String f:filess){
                    SysFileEntity sysFileEntity = new SysFileEntity();
                    sysFileEntity.setAttachMent(f);
                    sysFileEntity.setTableid(sysNoticeEntity.getId());
                    sysFileEntity.setType(1);
                    sysFileEntity.setCreateTime(nowTime);
                    sysFileEntity.setUpdateTime(nowTime);
                    sysFileEntity.setCreateUser(id);
                    sysFileEntity.setUpdateUser(id);
                    this.save(sysFileEntity);
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存公告里的附件
	 * @param sysNoticeEntity
	 * @param nowTime
     * @param id
     */
	@Override
	public void saveAttach(SysNoticeEntity sysNoticeEntity, String nowTime, String id) {
		try {
			int length= sysNoticeEntity.getAttachMent().length();
			if(length>0){
                String filess = sysNoticeEntity.getAttachMent().substring(0,length-1);
                String[] files=filess.split(",");
                for (int j=0;j<files.length;j++){
                    SysFileEntity sysFileEntity = new SysFileEntity();
                    sysFileEntity.setAttachMent(files[j]);
                    sysFileEntity.setTableid(sysNoticeEntity.getId());
                    sysFileEntity.setType(1);
                    sysFileEntity.setCreateTime(nowTime);
                    sysFileEntity.setUpdateTime(nowTime);
                    sysFileEntity.setCreateUser(id);
                    sysFileEntity.setUpdateUser(id);
                    this.save(sysFileEntity);
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改消息里的附件
	 * @param sysMessageEntity
	 * @param nowTime
     * @param id
     */
	@Override
	public void updateMeAttach(SysMessageEntity sysMessageEntity, String nowTime, String id) {
		try {
			int lengths= sysMessageEntity.getAttachMent().length();
			if(lengths>0){
                String file = sysMessageEntity.getAttachMent().substring(0,lengths-1);
                String[] filess=file.split(",");
                for(String f:filess){
                    SysFileEntity sysFileEntity = new SysFileEntity();
                    sysFileEntity.setAttachMent(f);
                    sysFileEntity.setTableid(sysMessageEntity.getId());
                    sysFileEntity.setType(2);
                    sysFileEntity.setCreateTime(nowTime);
                    sysFileEntity.setUpdateTime(nowTime);
                    sysFileEntity.setCreateUser(id);
                    sysFileEntity.setUpdateUser(id);
                    this.save(sysFileEntity);
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存消息里的附件
	 * @param sysMessageEntity
	 * @param nowTime
     * @param id
     */
	@Override
	public void saveMeAttach(SysMessageEntity sysMessageEntity, String nowTime, String id) {
		try {
			int length= sysMessageEntity.getAttachMent().length();
			if(length>0){
                String filess = sysMessageEntity.getAttachMent().substring(0,length-1);
                String[] files=filess.split(",");
                for (int j=0;j<files.length;j++){
                    SysFileEntity sysFileEntity = new SysFileEntity();
                    sysFileEntity.setAttachMent(files[j]);
                    sysFileEntity.setTableid(sysMessageEntity.getId());
                    sysFileEntity.setType(2);
                    sysFileEntity.setCreateTime(nowTime);
                    sysFileEntity.setUpdateTime(nowTime);
                    sysFileEntity.setCreateUser(id);
                    sysFileEntity.setUpdateUser(id);
                    this.save(sysFileEntity);
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
